var qqmapsdk,
  api = require("../../../../api.js"),
  QQMapWX = require('../../../../utils/qqmap-wx-jssdk.min.js'),
  app = getApp();
const {
  $Toast
} = require('../../../../iview/base/index');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    default: false,
    /** 选择的标签id */
    tag: '家庭',
    autosize: {
      maxHeight: 100,
      minHeight: 50
    },
    name: '',
    phone: '',
    provice: '',
    city: '',
    area: '',
    address: '',
    /** 返回的跳转地址 */
    back_url: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      /** 自己的key秘钥 http://lbs.qq.com/console/mykey.html 在这个网址申请 */
      key: 'T7MBZ-4EBC6-E3ZSY-EFZMG-BP5RF-FCFOC'
    });
    console.log('options.back_url',decodeURIComponent(options.back_url))
    if(options.back_url != undefined){
      this.setData({
        back_url: decodeURIComponent(options.back_url) 
      })
    }
  },

  onChange(event) {
    const detail = event.detail;
    this.setData({
      default: detail.value
    })
  },

  /** 选择标签 */
  chose_tag(e) {
    console.log(e)
    var id = e.currentTarget.id;
    this.setData({
      tag: id,
    })
  },

    /** 输入框的值传输 */
    bind_key_input: function (e) {
      let item = e.currentTarget.dataset.model;
      console.log(e)
      this.setData({
        [item]: e.detail
      });
    },
    bind_key_input_: function (e) {
      let item = e.currentTarget.dataset.model;
      console.log(e)
      this.setData({
        [item]: e.detail.value
      });
    },

  /** 添加地址 */
  add_address: function () {
    var is_defualt = '0'
    var _this = this;
    if (_this.data.name != '' && _this.data.phone != '' &&
      _this.data.provice != '' && _this.data.address != '') {
      if (this.data.default) {
        is_defualt = '1'
      } else {
        is_defualt = '0'
      }
      var t = {
        openId: getApp().globalData.wxUserInfo.openId,
        name: _this.data.name,
        phone: _this.data.phone,
        provice: _this.data.provice,
        city: _this.data.city,
        area: _this.data.area,
        address: _this.data.address,
        tag: _this.data.tag,
        isDefault: is_defualt
      };
      /** 保存地址 */
      wx.request({
        url: api.user.add_address,
        data: t,
        success: function (res) {
          console.log(res)
          setTimeout(() => {
            $Toast({
              content: '地址保存成功',
              type: 'success'
            });
          }, 1000);
          setTimeout(() => {
            if(_this.data.back_url.length != 0 ){
              console.log(`${_this.data.back_url}`)
              wx.navigateTo({
                url: `${_this.data.back_url}&address_id=${res.data.data}`,
              })
            }else{
              wx.navigateTo({
                url: '/pages/mine/address/address',
              })
            }
          }, 1000);
        }
      });
    } else {
      $Toast({
        content: '请填写完整',
        icon: 'delete'
      });
    }
  },

  /** 选择地图 */
  select_address() {
    var that = this
    wx.getSetting({
      success(res) {
        console.log('res是否开启位置授权', res)
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              wx.chooseLocation({
                success: res => {
                  that.getAddress(res.latitude, res.longitude)
                }
              })
            },
            /** 失败 */
            fail() {}
          })
        } else {
          // 已经授权了就会直接进入地图
          wx.chooseLocation({
            success: res => {
              that.getAddress(res.latitude, res.longitude)
            }
          })
        }
      }
    })
  },
  /** 获取地理位置 */
  getAddress: function (latitude, longitude) {
    let that = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        console.log(res);
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        let district = res.result.ad_info.district
        let detail = res.result.formatted_addresses.recommend
        that.setData({
          latitude: latitude,
          longitude: longitude,
          provice: province,
          city: city,
          area: district,
          address: detail
        })
      },
      fail: function (res) {
        console.log("fail");
        console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });
  },


})